<?php
namespace BoxEng\Controller\Admin;
//use Think\Controller;

class IndexController extends CommonController
{
    public function _initialize()
    {
        parent::_initialize();
        $this->subtitle = BoxModel("eng_subtitle");
        $this->subtitle_words = BoxModel("eng_subtitle_words");
    }


    public function index()
    {
        $data = $this->subtitle->get("is_show=1", 8, "id desc");
        $this->data = $data['list'];
        $this->show_page($data['pager']);
        $this->display();
    }

    public function add()
    {
        $this->display();
    }

    public function add_do()
    {
        $row = array(
            'section' => I('section'),
            'add_time' => date("Y-m-d H:i:s"),
            'path' => I('path'),
        );
        $subtitle_id = $this->subtitle->add($row);
        $file = $_FILES['file'];
        $file_type = I('file_type');//文件类型
        $word_type = I('word_type');//英语/中文
        $tmp_name = $file['tmp_name'];
        $content = file_get_contents($tmp_name);
        $bool = json_encode($content);

        if (!$bool || $bool == "null") {
            $content = iconv("GBK", "UTF-8", $content);
        }
        $pat = '/{(.*?)}/i';
        $content = preg_replace($pat, "", $content);
        $item = explode("\n", $content);
        $rows = array();
        /*****ass文件****/
        if ($file_type == 1) {
            foreach ($item as $one) {
                $data = explode("\N", $one);
                $data2 = explode(",", $data[0]);
                $row = array();
                //dump($data2);
                $row['start_time'] = str_replace(",", ".", $data2['1']);
                $row['end_time'] = str_replace(",", ".", $data2['2']);
                if ($word_type == 2) {//中文/英语
                    $row['english'] = $data[1];
                    $row['chinese'] = $data2[9];
                }
                if ($word_type == 1) {//英语/中文
                    $row['chinese'] = $data[1];
                    $row['english'] = $data2[9];
                }
                $rows[] = $row;
            }
        }
        /********srt文件***/
        if ($file_type == 2) {
            foreach ($item as $k => $one) {
                $row = array();
                if ($one > 0) {
                    $str_time = $item[$k + 1];
                    $data2 = explode("-->", $str_time);
                    $row['start_time'] = str_replace(",", ".", $data2['0']);
                    $row['end_time'] = str_replace(",", ".", $data2['1']);
                    if ($word_type == 2) {//中文/英语
                        $row['chinese'] = $item[$k + 2];
                        $row['english'] = $item[$k + 3];
                    }
                    if ($word_type == 1) {//英语/中文
                        $row['english'] = $item[$k + 2];
                        $row['chinese'] = $item[$k + 3];
                    }
                }

                $rows[] = $row;
            }

        }
        header("Content-type: text/html; charset=utf-8");
// dump($rows);exit();
        /*****保存字幕**/
        foreach ($rows as $row) {
            if (trim($row['english']) && trim($row['chinese']) && trim($row['start_time']) && trim($row['end_time'])) {
                $row['subtitle_id'] = $subtitle_id;
                $minute = I('minute', '10', "intval");
                $row['part_id'] = $this->subtitle_words->get_page($row['start_time'], $minute);
                $this->subtitle_words->add($row);
            }
        }
        $this->jump("保存成功", U('index'));

    }

    public function show_words()
    {
        $id = I('id');
        $this->subData = $this->subtitle->where(array('id' => $id))->find();
        $data = $this->subtitle_words->get("subtitle_id='$id'", "10", "id");
        $this->data = $data['list'];
        $this->show_page($data['pager']);
        $this->display();
    }

    function del_words()
    {
        $id = I('id');
        $this->subtitle_words->where(array('id' => $id))->delete();
        $this->jump("删除成功");
    }


    public function del()
    {
        $id = I('id');
        $this->subtitle->where(array('id' => $id))->delete();
        $this->subtitle_words->where(array('subtitle_id' => $id))->delete();
        $this->jump("删除成功");
    }

    public function change()
    {

        header("Content-type: text/html; charset=utf-8");
        if (IS_POST) {
            $file = $_FILES['file'];
            $name = $file['name'];
            $filename = $file['tmp_name'];
            header("Accept-Length: " . $size);
            $content = file_get_contents($filename);
            $par = "/[\x80-\xff]/";
            $content = preg_replace($par, "", $content);//过滤中文
            $content = str_replace("{\\fn\\fs24}", "", $content);
            $content = str_replace("{\\r}", "", $content);
            $content = str_replace("\N", "", $content);
            file_put_contents(RUNTIME_PATH . "/" . $name, $content);
            download(RUNTIME_PATH . "/" . $name);
            exit();
        }
        //file_put_contents($file,$content);
        $this->display();
    }

    public function down_change()
    {
        header("Content-type: text/html; charset=utf-8");
        $this->id = I('id');
        $subtitle_data = BoxModel("eng_subtitle")->load($this->id);
        if (IS_POST) {
            $data = $this->subtitle_words->load_words($this->id);
            $name = I('file_name');
            $info = pathinfo($name);
            $content = "";
            $path = $info['filename'];
            foreach ($data as $k => $one) {
                $count = count($data[$k]);
                $start_time = $data[$k][0]['start_time'];
                $end_time = $data[$k][$count - 1]['end_time'];
                $long = date("H:i:s", (strtotime($end_time) - strtotime($start_time) + strtotime("00:00:05")));
                if (strtotime($start_time) - strtotime("00:00:00") > 4) {
                    $start_time = date("H:i:s", strtotime($start_time) - 5);
                }
                $i = $k - 1;
                $out_put = $path . "\\" . $i . "_600x360.mp4";
                //$out_put1=$path."/".$i."_640x360.mp4";
                $out_put2 = $path . "\\" . $i . "_848x476.mp4";
                $out_put3 = $path . "\\" . $i . ".jpg";
                $out_put4 = $path . "\\" . $i . ".mp3";
                $content .= "ffmpeg -ss {$start_time} -i {$name} -r 23 -t {$long} -b:v 300k -s 600x360 -preset superfast -x264opts keyint=10 {$out_put} \n";//SD
                //$content.="ffmpeg -ss {$start_time} -i {$name} -r 23 -t {$long} -b:v 242k -s 640x360 -preset superfast -x264opts keyint=10 {$out_put1} \n";//SD
                $content .= "ffmpeg -ss {$start_time} -i {$name} -r 23 -t {$long} -b:v 650k -s 848x476 -preset superfast -x264opts keyint=10 {$out_put2} \n";//HD
                $content .= "ffmpeg -ss {$start_time} -i {$name} -f image2 -y {$out_put3} \n";
                $content .= "ffmpeg -ss {$start_time} -i {$name} -t {$long} -vn -ar 44100 -ac 2 -ab 192 -f mp3 {$out_put4} \n";

            }
            //dump($contntet);exit();
            $save_name = RUNTIME_PATH . "/" . $subtitle_data['path'] . ".bat";
            file_put_contents($save_name, $content);
            download($save_name);
            exit();
        }
        //ffmpeg -ss 00:50:57 -i C:\Users\Administrator\Desktop\change\1.mkv -t 30 -vn -ar 44100 -ac 2 -ab 192 -f mp3 C:\Users\Administrator\Desktop\change\1\5.mp3

        //ffmpeg  -i D:\1_848x476.mp4 -r 23 -b:v 242k -s 640x360 -preset superfast -x264opts keyint=25   -vcodec copy –an D:\2_848x476.mp4
        //ffmpeg -i D:\0_600x360.mp4  -vn -ar 44100 -ac 2 -ab 192 -f mp3 D:\1.mp3
        //file_put_contents($file,$content);
        //ffmpeg -ss 00:02:06 -i D:\1.mp4 -f image2 -y test1.jpg
        //ffmpeg  -i D:\1.mp4 -i D:\1.ass -map 0:0 -map 0:1 -map 1 -c:a copy -c:v copy -c:s copy  D:\2.mp4
        //ffmpeg  -i D:\11.mp4 -r 23 -b:v 242k -s 640x360 -preset superfast -x264opts keyint=5 D:\2.mp4
        //qt-faststart D:\2.mp4 D:\2.mp4  -vcodec copy –an
        //C:\Users\Administrator>ffmpeg -i C:\.狮子王.mkv -map 0:s:0 D:\sub1.srt
        $this->display();
    }

    /*****下载英语字幕****/
    function down_srt()
    {
        $id = I('id');
        $data = $this->subtitle->load($id);
        $items = $this->subtitle_words->get("subtitle_id='$id'", "", "id");
        $save_name = RUNTIME_PATH . $data['section'] . ".srt";
        $content = "WEBVTT\n\n";
        foreach ($items as $k => $item) {
            $k = $k + 1;
            $content .= $k . "\n";
            $content .= $item['start_time'] . " --> " . $item['end_time'] . "\n";
            $content .= "<c.subtitle>" . $item['english'] . "</c>\n";
            $content .= "\n";
        }
        file_put_contents($save_name, $content);
        download($save_name);
        exit();
    }


}